//
//  PageSixViewController.m
//  CMNavBar
//
//  Created by Administrator on 5/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PageSixViewController.h"
#import "XMLParser.h"
#import "CarBean.h"

@implementation PageSixViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"PageSixTitle", @"");
    }
    return self;
}

- (void)dealloc
{
    [myTableView release];
    [indicator release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [myTableView release];
    myTableView = nil;
    [indicator release];
    indicator = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewDidAppear:(BOOL)animated
{
    appDelegate = (CMNavBarAppDelegate *)[[UIApplication sharedApplication] delegate];
    
    //reading the file.
	NSString *filePath = [[NSBundle mainBundle] pathForResource:@"carData" ofType:@"xml"];  
	NSData *fileData = [NSData dataWithContentsOfFile:filePath]; 
    
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:fileData];
    
    //Initialize the delegate.
    XMLParser *parser = [[XMLParser alloc] initXMLParser];
    
    //Set delegate
    [xmlParser setDelegate:parser];
    
    //Start parsing the XML file.
    BOOL success = [xmlParser parse];
    
    if(success) {
        NSLog(@"No Errors");
        [myTableView reloadData];
        // hide indicator
        [indicator stopAnimating];
    }
    else
        NSLog(@"Error Error Error!!!");
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark - TableView Callback method

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [appDelegate.cars count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyIdentifier = @"MyIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] autorelease];
	} 
	
	// Set up the cell
	int storyIndex = indexPath.row;
    CarBean *temp = [appDelegate.cars objectAtIndex:storyIndex];
    cell.textLabel.text = [temp.dataDictionary objectForKey: @"title"];
    cell.detailTextLabel.text = [temp.dataDictionary objectForKey: @"description"];
    
    if (cell.imageView.image == nil) {
        id path = [temp.dataDictionary objectForKey: @"image"];
        NSURL *url = [NSURL URLWithString:path];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *img = [[UIImage alloc] initWithData:data];
        cell.imageView.image = img;
        [img release];
    }
    
	return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    int storyIndex = indexPath.row;
    CarBean *temp = [appDelegate.cars objectAtIndex:storyIndex];
    NSString *title = [temp.dataDictionary objectForKey: @"title"];
    NSString *message = [temp.dataDictionary objectForKey: @"description"];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}

@end
